package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.LoginInfo;
import com.itheima.pojo.domain.Emp;
import com.itheima.pojo.domain.EmpExpr;
import com.itheima.pojo.domain.EmpLog;
import com.itheima.pojo.vo.PageResult;
import com.itheima.pojo.dto.EmpQueryParamDTO;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * 员工管理
 */
@Service
@Slf4j
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageResult list(EmpQueryParamDTO empQueryParamDto) {
        //开始分页
        PageHelper.startPage(empQueryParamDto.getPage(),empQueryParamDto.getPageSize());
        //查询分页数据
        Page<Emp> emps= empMapper.list(empQueryParamDto);
        //封装分页结果
        PageResult<Emp> pageResult = new PageResult<>(emps.getTotal(), emps.getResult());
        return pageResult;
    }

    @Override
    public List<Emp> listAll() {

        List<Emp> emps = empMapper.listAll();
        return emps;
    }

    @Override
    public Emp getEmpInfoById(Integer id) {
        //查询emp信息
        Emp emp = empMapper.listById(id);
        //查询员工工作经验信息
        List<EmpExpr> empExprs = empMapper.listExprByid(id);
        //将员工工作经验信息设置到emp对象中
        emp.setExprList(empExprs);

        return emp;
    }

    @Override
    //rollbackFor参数是该注解的一个关键属性，用于指定哪些异常类型应触发事务回滚
    @Transactional(rollbackFor = Exception.class)
    public void addEmpInfo(Emp emp) {
        try {
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insertEmp(emp);
            Integer id = emp.getId();
            List<EmpExpr> exprList = emp.getExprList();
            for (EmpExpr empExpr : exprList) {
                empExpr.setEmpId(id);
            }
            if (exprList != null && !exprList.isEmpty()){
                    empExprMapper.insertEmpExpr(exprList);
            }
        } finally {
            EmpLog empLog = new EmpLog(null, LocalDateTime.now(), "添加员工" + emp.getName());
            empLogService.insertLog(empLog);
        }

    }

    /**
     * 修改员工信息
     * @param emp
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmpInfo(Emp emp) {
        //修改员工信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmpInfo(emp);

        //获取传入的员工工作经历集合
        List<EmpExpr> exprList = emp.getExprList();
        //从传入的参数中获取员工id
        Integer id = emp.getId();

        //删除员工原来的工作经历
        empExprMapper.deleteEmpExperInfo(id);
        //将新的工作经历插入数据库
        if (!exprList.isEmpty() ) {
            exprList.forEach(expr -> expr.setEmpId(id));
            empExprMapper.insertEmpExpr(exprList);

        }



    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEmpInfo(List<Integer> ids) {

        //先删除员工表
        empMapper.deleteEmpInfo(ids);

        //在根据empid删除员工工作经历表
        empMapper.deleteEmpExperInfo(ids);

    }

    @Override
    public LoginInfo login(Emp emp) {
        Emp empLogin = empMapper.getUsernameAndPassword(emp);
        if(empLogin != null){
            //1.生成JWT令牌
            HashMap<String, Object> dateMap = new HashMap<>();
            dateMap.put("id", empLogin.getId());
            dateMap.put("username", empLogin.getUsername());
            String jwt = JwtUtils.generateJwt(dateMap);
            log.info("生成JWT令牌: {}", jwt);
            LoginInfo loginInfo = new LoginInfo(empLogin.getId(), empLogin.getUsername(), empLogin.getName(), jwt);
            return loginInfo;
        }
        return null;
    }
}